/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.ArrayList;
import java.util.List;
import modelo.Aluno;
import org.hibernate.classic.Session;
import persistence.HibernateUtil;

/**
 *
 * @author Sh4c4w
 */
public class AlunoDao {
    Session sessao = null;

    public void add(Aluno aluno) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(aluno);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public Aluno getProfById(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Aluno p = (Aluno) sessao.get(Aluno.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return p;
    }
    
    public void update (Aluno aluno){
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(aluno);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public List<Aluno> list() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        List<Aluno> lista = new ArrayList<Aluno>();
        lista = sessao.createQuery("from Pessoa where DTYPE = 'Aluno'").list();
        sessao.getTransaction().commit();
        sessao.close();
        return lista;
    }

    public boolean remove(Aluno aluno) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(aluno);
        sessao.getTransaction().commit();
        sessao.close();
        return true;
    }
}
